package com.ruoyi.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.teacher.domain.TOption;
import com.ruoyi.teacher.domain.TPaperQuestion;
import com.ruoyi.teacher.domain.TQuestion;
import com.ruoyi.teacher.mapper.TOptionMapper;
import com.ruoyi.teacher.mapper.TQuestionMapper;
import com.ruoyi.teacher.vo.TPaperQuestionVo;
import com.ruoyi.teacher.mapper.TPaperQuestionMapper;
import com.ruoyi.teacher.service.ITPaperQuestionService;
import net.sf.jsqlparser.statement.select.Top;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 试卷问题Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-03
 */
@Service
public class TPaperQuestionServiceImpl extends ServiceImpl<TPaperQuestionMapper, TPaperQuestion> implements ITPaperQuestionService {

    @Override
    public TableDataInfo<TPaperQuestionVo> queryList(TPaperQuestionVo entity) {
        return PageUtils.buildDataInfo(this.baseMapper.queryList(PageUtils.buildPage(), entity));
    }

    @Override
    public List<TPaperQuestionVo> queryAll(TPaperQuestionVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public TPaperQuestionVo queryById(Long id) {
        return this.baseMapper.queryById(id);
    }

    @Autowired
    TPaperQuestionMapper paperQuestionMapper;
    @Autowired
    TQuestionMapper questionMapper;
    @Autowired
    TOptionMapper optionMapper;
    @Override
    public List<List<TPaperQuestionVo>>  getQuestionByPId(Long pid) {
        QueryWrapper<TPaperQuestion> qw = new QueryWrapper<>();
        qw.eq("p_id",pid);
        List<TPaperQuestion> tPaperQuestions = paperQuestionMapper.selectList(qw);
        System.err.println(tPaperQuestions);
        List<List<TPaperQuestionVo>> lists = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            lists.add(new ArrayList<>());
        }
        for (TPaperQuestion tPaperQuestion : tPaperQuestions) {
//            根据问题id查询选项
            TQuestion tQuestion = questionMapper.selectById(tPaperQuestion.getId());
//            根据问题ID查询选项
            QueryWrapper<TOption> tOptionQueryWrapper = new QueryWrapper<>();
            tOptionQueryWrapper.eq("q_id",tQuestion.getId());
            List<TOption> tOptions = optionMapper.selectList(tOptionQueryWrapper);

            TPaperQuestionVo tPaperQuestionVo = new TPaperQuestionVo();
            BeanUtils.copyProperties(tPaperQuestion,tPaperQuestionVo);
            BeanUtils.copyProperties(tQuestion,tPaperQuestionVo);
            tPaperQuestionVo.setId(tQuestion.getId());
            tPaperQuestionVo.setOptions(tOptions);

//            String type = tPaperQuestionVo.getType();
            lists.get(tPaperQuestionVo.getOrderNum()-1).add(tPaperQuestionVo);
            System.err.println(lists+"123123");
        }
        return lists;
    }



}
